import { useMutation, useQuery } from "@tanstack/react-query";
import { client } from "../request";

export const useDownloadFileList = (
  filter_type: string,
  search: string,
  page: number
) => {
  return useQuery({
    queryKey: ["downloads", filter_type, search, page],
    queryFn: () => {
      let value = search.split(" ");
      let resultValue = "";
      if (value.length > 1 && value[0] != " ") {
        resultValue = value.join("_");
      } else {
        resultValue = search.trim();
      }

      return client
        .get({
          url: `/download/${filter_type}/?search=${resultValue || ""}&page=${
            page || 1
          }`,
        })
        .then(
          (res) => {
            res.results = res.results.map((res: any) => {
              res["key"] = res.name;
              return res;
            });
            return Promise.resolve(res);
          },
          (err) => {
            return Promise.reject(err);
          }
        );
    },
  });
};

// export const useDownloadFile = () => {
//   return useMutation({
//     mutationFn: (param: { filter_type: string; file_id: number }) => {
//       return client.getReturnHeader({
//         url: `/download/${param.filter_type}/${param.file_id}/download/`,
//         responseType: "blob",
//       });
//     },
//   });
// };

export const useDownloadFile = () => {
  return useMutation({
    mutationFn: (param: { filter_type: string; file_id: number }) => {
      return client.getReturnHeader({
        url: `/download/${param.filter_type}/${param.file_id}/exists/`,
      });
    },
  });
};

export const useDownloadStaticHelp = () => {
  return useMutation({
    mutationFn: (param: { file_name: string }) => {
      return client.getReturnHeader({
        url: `/help/static/${param.file_name}`,
      });
    },
  });
};

export const useDownResultFile = () => {
  return useMutation({
    mutationFn: ({ file_path }: { file_path: string }) => {
      return client.getReturnHeader({
        url: `/download_file/file_exists/`,
        params: { file_path },
      });
    },
  });
};

// axios({
//   method: 'post',
//   headers: {
//     'Content-Type': 'application/json; charset=utf-8'
//   },
//   url: '/robot/strategyManagement/analysisExcel',
//   responseType: 'blob',
//   headers: { //如果需要权限下载的话，加在这里
//         Authorization: '123456'
//     }
//   data: JSON.stringify(params),
// }).then(function(res){
//    var content = res.headers['content-disposition'];
//    var name = content && content.split(';')[1].split('filename=')[1];
//    var fileName = decodeURIComponent(name)
//    downloadFile(res.data,fileName)
// })

// downloadFile:function(data,fileName){
//   // data为blob格式
//   var blob = new Blob([data]);
//   var downloadElement = document.createElement('a');
//   var href = window.URL.createObjectURL(blob);
//   downloadElement.href = href;
//   downloadElement.download = fileName;
//   document.body.appendChild(downloadElement);
//   downloadElement.click();
//   document.body.removeChild(downloadElement);
//   window.URL.revokeObjectURL(href);
// }
